<html lang="en-US">

<head>
    
<title>log 标准库 - 花落雨忧</title>

<meta property="og:title" content="log 标准库 - 花落雨忧">



    



    
    <meta property="description" content="2018/09/28 20:03:08 EDDYCJY Blog... 构成 [日期]&amp;lt;空格&amp;gt;[时分秒]&amp;lt;空格&amp;gt;[内容]&amp;lt;\n&amp;gt;
[&amp;hellip;] type Logger struct { mu sync.Mutex prefix string flag int out io.Writer buf []byte }  mu：互斥锁， &amp;hellip;">
    <meta property="og:description" content="2018/09/28 20:03:08 EDDYCJY Blog... 构成 [日期]&amp;lt;空格&amp;gt;[时分秒]&amp;lt;空格&amp;gt;[内容]&amp;lt;\n&amp;gt;
[&amp;hellip;] type Logger struct { mu sync.Mutex prefix string flag int out io.Writer buf []byte }  mu：互斥锁， &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.site/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.site/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.site/js/jquery.min.js"></script>




<link href="https://www.lican.site/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>
        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/posts/go/pkg/2018-09-28-log/">log 标准库</a>
                                        </h2>
                                        
                                        <div class="post-byline">Published on
                                            <a class="date" href="javascript:;">2018/09/28</a>
                                            
                                            
                                            
                                            By 
                                            <a class="author" href="javascript:;">煎鱼</a></div>
                                            
                                        
                                    </div>
                                    <div class="post-content">
                                        <h2 id="日志">日志</h2>
<h3 id="输出">输出</h3>
<pre><code>2018/09/28 20:03:08 EDDYCJY Blog...
</code></pre><h3 id="构成">构成</h3>
<p>[日期]&lt;空格&gt;[时分秒]&lt;空格&gt;[内容]&lt;\n&gt;</p>
<h2 id="源码剖析">源码剖析</h2>
<h3 id="logger">Logger</h3>
<pre><code>type Logger struct {
	mu     sync.Mutex 
	prefix string
	flag   int
	out    io.Writer
	buf    []byte
}
</code></pre><ol>
<li>mu：互斥锁，用于确保原子的写入</li>
<li>prefix：每行需写入的日志前缀内容</li>
<li>flag：设置日志辅助信息（时间、文件名、行号）的写入。可选如下标识位：</li>
</ol>
<pre><code>const (
	Ldate         = 1 &lt;&lt; iota       // value: 1
	Ltime                           // value: 2
	Lmicroseconds                   // value: 4
	Llongfile                       // value: 8
	Lshortfile                      // value: 16
	LUTC                            // value: 32
	LstdFlags     = Ldate | Ltime   // value: 3
)
</code></pre><ul>
<li>Ldate：当地时区的格式化日期：2009/01/23</li>
<li>Ltime：当地时区的格式化时间：01:23:23</li>
<li>Lmicroseconds：在 Ltime 的基础上，增加微秒的时间数值显示</li>
<li>Llongfile：完整的文件名和行号：/a/b/c/d.go:23</li>
<li>Lshortfile：当前文件名和行号：d.go：23，会覆盖 Llongfile 标识</li>
<li>LUTC：如果设置 Ldate 或 Ltime，且设置 LUTC，则优先使用 UTC 时区而不是本地时区</li>
<li>LstdFlags：Logger 的默认初始值（Ldate 和 Ltime）</li>
</ul>
<ol start="4">
<li>out：io.Writer</li>
<li>buf：用于存储将要写入的日志内容</li>
</ol>
<h3 id="new">New</h3>
<pre><code>func New(out io.Writer, prefix string, flag int) *Logger {
	return &amp;Logger{out: out, prefix: prefix, flag: flag}
}

var std = New(os.Stderr, &quot;&quot;, LstdFlags)
</code></pre><p>New 方法用于初始化 Logger，接受三个初始参数，可以定制化而在 log 包内默认会初始一个 std，它指向标准输入流。而默认的标准输出、标准错误就是显示器（输出到屏幕上），标准输入就是键盘。辅助的时间信息默认为 <code>Ldate | Ltime</code>，也就是 <code>2009/01/23 01:23:23</code></p>
<pre><code>// os
var (
	Stdin  = NewFile(uintptr(syscall.Stdin), &quot;/dev/stdin&quot;)
	Stdout = NewFile(uintptr(syscall.Stdout), &quot;/dev/stdout&quot;)
	Stderr = NewFile(uintptr(syscall.Stderr), &quot;/dev/stderr&quot;)
)
</code></pre><ul>
<li>Stdin：标准输入</li>
<li>Stdout：标准输出</li>
<li>Stderr：标准错误</li>
</ul>
<h3 id="getter">Getter</h3>
<ul>
<li>Flags</li>
<li>Prefix</li>
</ul>
<h3 id="setter">Setter</h3>
<ul>
<li>SetFlags</li>
<li>SetPrefix</li>
<li>SetOutput</li>
</ul>
<h3 id="print-fatal-panic">Print*, Fatal*, Panic*</h3>
<pre><code>func Print(v ...interface{}) {
	std.Output(2, fmt.Sprint(v...))
}

func Printf(format string, v ...interface{}) {
	std.Output(2, fmt.Sprintf(format, v...))
}

func Println(v ...interface{}) {
	std.Output(2, fmt.Sprintln(v...))
}

func Fatal(v ...interface{}) {
	std.Output(2, fmt.Sprint(v...))
	os.Exit(1)
}

func Panic(v ...interface{}) {
	s := fmt.Sprint(v...)
	std.Output(2, s)
	panic(s)
}

...
</code></pre><p>这一部分介绍最常用的日志写入方法，从源码可得知 <code>Xrintln</code>、<code>Xrintf</code> 函数 <strong>换行</strong>、<strong>可变参数</strong>都是通过 <code>fmt</code> 标准库的方法去实现的</p>
<p><code>Fatal</code> 和 <code>Panic</code> 是通过 <code>os.Exit(1)</code>、<code>panic(s)</code> 集成实现的。而具体的组装逻辑是通过 <code>Output</code> 方法实现的</p>
<h4 id="loggeroutput">Logger.Output</h4>
<pre><code>func (l *Logger) Output(calldepth int, s string) error {
	now := time.Now() // get this early.
	var file string
	var line int
	l.mu.Lock()
	defer l.mu.Unlock()
	if l.flag&amp;(Lshortfile|Llongfile) != 0 {
		// Release lock while getting caller info - it's expensive.
		l.mu.Unlock()
		var ok bool
		_, file, line, ok = runtime.Caller(calldepth)
		if !ok {
			file = &quot;???&quot;
			line = 0
		}
		l.mu.Lock()
	}
	l.buf = l.buf[:0]
	l.formatHeader(&amp;l.buf, now, file, line)
	l.buf = append(l.buf, s...)
	if len(s) == 0 || s[len(s)-1] != '\n' {
		l.buf = append(l.buf, '\n')
	}
	_, err := l.out.Write(l.buf)
	return err
}
</code></pre><p>Output 方法，简单来讲就是将写入的日志事件信息组装并输出，它会根据 flag 标识位的不同来使用 <code>runtime.Caller</code> 去获取当前 goroutine 所执行的函数文件、行号等调用信息（log 标准库中默认深度为 2）。另外如果结尾不是换行符 <code>\n</code>，将自动补全一个换行</p>
<h4 id="loggerformatheader">Logger.formatHeader</h4>
<pre><code>func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {
	*buf = append(*buf, l.prefix...)
	if l.flag&amp;(Ldate|Ltime|Lmicroseconds) != 0 {
		if l.flag&amp;LUTC != 0 {
			t = t.UTC()
		}
		if l.flag&amp;Ldate != 0 {
			year, month, day := t.Date()
			itoa(buf, year, 4)
			*buf = append(*buf, '/')
			itoa(buf, int(month), 2)
			*buf = append(*buf, '/')
			itoa(buf, day, 2)
			*buf = append(*buf, ' ')
		}
		if l.flag&amp;(Ltime|Lmicroseconds) != 0 {
			hour, min, sec := t.Clock()
			itoa(buf, hour, 2)
			*buf = append(*buf, ':')
			itoa(buf, min, 2)
			*buf = append(*buf, ':')
			itoa(buf, sec, 2)
			if l.flag&amp;Lmicroseconds != 0 {
				*buf = append(*buf, '.')
				itoa(buf, t.Nanosecond()/1e3, 6)
			}
			*buf = append(*buf, ' ')
		}
	}
	if l.flag&amp;(Lshortfile|Llongfile) != 0 {
		if l.flag&amp;Lshortfile != 0 {
			short := file
			for i := len(file) - 1; i &gt; 0; i-- {
				if file[i] == '/' {
					short = file[i+1:]
					break
				}
			}
			file = short
		}
		*buf = append(*buf, file...)
		*buf = append(*buf, ':')
		itoa(buf, line, -1)
		*buf = append(*buf, &quot;: &quot;...)
	}
}
</code></pre><p>该方法主要是用于格式化日志头（前缀），根据入参不同的标识位，添加分隔符和对应的值到日志信息中。执行流程如下：</p>
<p>（1）如果不是空值，则将 prefix 写入 buf</p>
<p>（2）如果设置 <code>Ldate</code>、<code>Ltime</code>、<code>Lmicroseconds</code>，则对应将日期和时间写入 buf</p>
<p>（3）如果设置 <code>Lshortfile</code>、<code>Llongfile</code>，则对应将文件和行号信息写入 buf</p>
<h4 id="loggeritoa">Logger.itoa</h4>
<pre><code>func itoa(buf *[]byte, i int, wid int) {
	// Assemble decimal in reverse order.
	var b [20]byte
	bp := len(b) - 1
	for i &gt;= 10 || wid &gt; 1 {
		wid--
		q := i / 10
		b[bp] = byte('0' + i - q*10)
		bp--
		i = q
	}
	// i &lt; 10
	b[bp] = byte('0' + i)
	*buf = append(*buf, b[bp:]...)
}
</code></pre><p>该方法主要用于将整数转换为定长的十进制 ASCII，同时给出负数宽度避免左侧补 0。另外会以相反的顺序组合十进制</p>
<h3 id="如何定制化-logger">如何定制化 Logger</h3>
<p>在标准库内，可通过其开放的 New 方法来实现各种各样的自定义 Logger 组件，但是为什么也可以直接 <code>log.Print*</code> 等方法呢？</p>
<pre><code>func New(out io.Writer, prefix string, flag int) *Logger
</code></pre><p>其实是在标准库内，如果你刚刚细心的看了前面的小节，不难发现其默认实现了一个 Logger 组件</p>
<pre><code>var std = New(os.Stderr, &quot;&quot;, LstdFlags)
</code></pre><p>这也是一个小小的精妙之处 ⭕️</p>
<h2 id="总结">总结</h2>
<p>通过查阅 log 标准库的源码，可得知最简单的一个日志包应该如何编写。另外 log 包是在所有涉及到 Logger 的地方都对 <code>sync.Mutex</code> 进行操作（以此解决原子问题），其余逻辑均为组装日志信息和转换数值格式，该包较为经典，可以多读几遍 😄</p>
<h2 id="问题">问题</h2>
<p>为什么在调用 <code>runtime.Caller</code> 前要先解锁，后再加锁呢?</p>


                                        
                                        
                                        
                                        <div class="rp4wp-related-posts">
                                            <h3>相关文章</h3>
                                            <ul>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/grpc/2018-09-24-stream-client-server/">「连载三」gRPC Streaming, Client and Server</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/grpc/2018-09-23-client-and-server/">「连载二」gRPC Client and Server</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/grpc/2018-09-22-install/">「连载一」gRPC及相关介绍</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/tools/2018-09-15-go-tool-pprof/">Go 大杀器之性能剖析 PProf</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/gin/2018-09-01-nginx/">「连载十七」用Nginx部署Go应用</a>
                                                    </div>
                                                </li>
                                                
                                            </ul>
                                        </div>
                                        
                                        
                                    </div>

                                    
                                    
                                    

                                    
                                    <div class="post-meta">
                                        
                                        
                                        <div class="post-tags">
                                            <ul>
                                            
                                            <li>
                                                <a href="/tags/go" title="View all posts tagged match">go</a>
                                            </li>
                                            
                                            <li>
                                                <a href="/tags/%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90" title="View all posts tagged match">源码分析</a>
                                            </li>
                                            
                                            </ul>
                                        </div>
                                        
                                        
                                        <nav class="further-reading">
                                            
                                            <div class="previous">
                                                <span>&lt;&lt; Prev</span>
                                                <a href="https://www.lican.site/posts/go/grpc/2018-09-24-stream-client-server/"
                                                    rel="prev">「连载三」gRPC Streaming, Client and Server</a> </div>
                                            
                                            
                                            <div class="next">
                                                <span>Next >></span>
                                                <a href="https://www.lican.site/posts/go/grpc/2018-10-07-grpc-tls/">「连载四」TLS 证书认证</a> 
                                            </div>
                                            
                                        </nav>
                                    </div>
                                    
                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.site/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.vip All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>